<template>
    <div >
      <zh-card title="">
      <zh-form ref="formRef" :md="12" :lg="12" :xl="12"  :model="classInfo" :labelWidth="120">  
        <zh-form-input prop="fileClassTitle" label="文件分类名称"  v-model="classInfo.fileClassTitle"   :disabled=true></zh-form-input>    
        <zh-form-select
              prop="appId"
              :lazy="true"
              label="所属应用"
              v-model="classInfo.appId"
              :fieldNames="{ label: 'appName', value: 'appId' }"
              funcCode="application0012"
              searchField="appName"
          ></zh-form-select>
        <zh-form-input prop="fieldName" label="字段名称"  v-model="classInfo.fieldName"   :disabled=false></zh-form-input>
        <zh-form-select
            :disabled="props.displayFlag"
            prop="secrecyLevel"
            label="保密级别"
            :options="state.dictData.SECRECYLEVEL"
            v-model="classInfo.secrecyLevel"
        ></zh-form-select>

        <zh-form-input prop="archivesClassCode" label="对应档案编号"  v-model="classInfo.archivesClassCode"   :disabled=false></zh-form-input>
        <zh-form-query v-model="state.advanced" @queryClick="queryFileClass" :formRef="formRef">
          <zh-button buttonType="add" @click="addAppClass">授权应用新增</zh-button>
        </zh-form-query>
      </zh-form>
      </zh-card>
      <zh-table title="文件列表信息" ref="fileHomeInfoTable" size="mini"  :url="VITE_FILE_API_URL+'fileFieldClass0001'"
            :columns="state.tableColumns" highlight-current-row>
            <template #op="scope">
                <el-tooltip content="我要申请" placement="top">
                    <el-button :icon="Edit" link @click="shenqing(scope.row)"></el-button>
                </el-tooltip>                

            </template>
        </zh-table>
        <zh-dialog
          ref="appClassDialog"
          v-model="state.appClassVisible"
          title="应用分类授权信息"
          width="30%"
          @confirm="saveAppClass">
            <FileAppClassInfo :displayFlag="false" ref="fileAppClassInfo">
            </FileAppClassInfo>
        </zh-dialog>
        <zh-dialog
          ref="appClassDialog"
          v-model="state.fileAppReadVisible"
          title="授权读取文件信息"
          width="30%"
          @confirm="saveFileAppRead">
            <FileAppReadInfo :displayFlag="false" ref="fileAppReadInfo">
            </FileAppReadInfo>
        </zh-dialog>
    </div>
</template>

<script lang="ts" setup name="fileHomeInfo">
import {FormInstance} from "element-plus";
import {  Edit } from '@element-plus/icons-vue'
import ZhCard from "@/components/zh-card/index.vue";
import FileAppClassInfo from '@/views/fileCenter/fileClass/component/FileAppClassInfo.vue';
import FileAppReadInfo from '@/views/fileCenter/fileHome/component/FileAppReadInfo.vue';
const VITE_FILE_API_URL = import.meta.env.VITE_FILE_API_URL as string+'/doc/api/';

// ------------------变量区------------------------------
const props = defineProps({
  scrollHeight: {
    type: String,
    default: ''
  },
  displayFlag: {
    type: Boolean,
    default:false
  },
});
const formRef = ref<FormInstance>();
const classInfo = reactive<any>({});
const fileHomeInfoTable = ref();
const fileAppClassInfo=ref();
const fileAppReadInfo=ref();

const state = reactive({
  advanced: false,
  dictData:[] as any,
  appClassVisible:false,
  fileAppReadVisible:false,
  
  tableColumns: [
        { type: 'seq', width: 60, fixed: 'left', align: 'center', title: '序号' },
       
        { field: 'appId', title: 'appId', visible: false, fixed: 'left',align: 'center', width: 120 },
        { field: 'appName', title: '所属应用', visible: true, fixed: 'left',align: 'center', width: 120 },
        { field: 'classTitle', title: '分类标题', visible: true, fixed: 'left',align: 'center', width: 120 },
        { field: 'fileClassCode', title: '分类编号', visible: true, fixed: 'left',align: 'center', width: 150 },
        { field: 'secrecyLevel', title: 'secrecyLevel', visible: false, fixed: 'left',align: 'center', width: 120 },
        { field: 'secrecyLevelName', title: '保密级别', visible: true, fixed: 'left',align: 'center', width: 120 },        
        { field: 'authStatusName', title: '审核状态', visible: false, fixed: 'left',align: 'center', width: 120 },
        { field: 'fileType', title: '文件类型限制', visible: true, fixed: 'left',align: 'center', width: 120 },
        { field: 'maxSize', title: '文件大小限制', visible: true, fixed: 'left',align: 'center', width: 120 },
        { field: 'archivesClassCode', title: '对应档案编号', visible: true, fixed: 'left',align: 'center', width: 120 },        
        { field: 'fileClassPath', title: '存放基础路径', visible: true, fixed: 'left',align: 'center', width: 150 },

        {
            field: 'op',
            title: '操作',
            visible: true,
            align: 'center',
            width: 100,
            fixed: 'right',
            slots: { default: 'op' }
        },
    ],
});

//------------------初始化------------------------------
const params = {
  dictFields: 'status,flag,SECRECYLEVEL',
};
$api.common.getBatchDict.post(params).then((res) => {
  state.dictData = res.result;
});


//--------------------------定义调用方法--------------------------------------------------
function shenqing(row) {
  console.log('开始申请：'+row.fileClassCode);   
  if(row.fileClassCode){
    state.fileAppReadVisible=true;
    let info={
      fileClassCode:row.fileClassCode,
      classTitle:row.classTitle,
      permitAppId:row.appId
    }
    nextTick(() => {
      fileAppReadInfo.value.setInitData(info);
    });

  }else{
    $messageBox.warning('请先选择列表信息', '提示');
    return false
  }
}

//保存新增的文件读取授权
function saveFileAppRead(){  
  //获取子页面的数据
  let appReadInfo = {appId:'',expireTime:0,fileClassCode:'',permitAppId:'',authStatus:'' };
  console.log(appReadInfo);
  //默认未审核
  appReadInfo.authStatus = '0';
  appReadInfo.appId=fileAppReadInfo.value.fileAppReadInfo.appId;
  appReadInfo.fileClassCode=fileAppReadInfo.value.fileAppReadInfo.fileClassCode;
  appReadInfo.permitAppId=fileAppReadInfo.value.fileAppReadInfo.permitAppId;
  let dateStr = fileAppReadInfo.value.fileAppReadInfo.expireTime;
  var date=new Date(dateStr);
  appReadInfo.expireTime= date.getTime();
  $api.common.fileSend.post('fileAppRead0004', appReadInfo).then(() => {
    $messageBox.success('新增成功', '提示');
    state.fileAppReadVisible=false;
    queryFileClass();
  }).catch(() => {});  
}


const queryFileClass = () => {
  if(classInfo.fileClassCode === null || classInfo.fileClassCode === undefined|| classInfo.fileClassCode === ''){
    $messageBox.warning('请先选择需要操作的文件分类', '提示');
    return false
  }
    fileHomeInfoTable.value.query(classInfo);
}

function setInitData(info) {
  formRef.value?.resetFields();
  classInfo.fileClassCode = info.fileClassCode;  
  classInfo.fileClassTitle = info.fileClassTitle;
  console.log('传入参数：'+classInfo.fileClassCode+';fileClassTitle:'+classInfo.fileClassTitle)
  
}

function addAppClass(){
  //获取选中的文件分类节点
  if(classInfo.fileClassCode){
    state.appClassVisible=true;
    let info={
      fileClassCode:classInfo.fileClassCode,
      fileClassTitle:classInfo.fileClassTitle
    }
    nextTick(() => {
      fileAppClassInfo.value.setInitData(info);
    });

  }else{
    $messageBox.warning('请先选择需要操作的文件分类', '提示');
    return false
  }
}

//保存新增的应用文件分类授权
function saveAppClass(){  
  //获取子页面的数据
  let appClassInfo = {appId:'',expireTime:0,fileClassCode:'',fileClassPath:'',authStatus:'' };
  console.log(appClassInfo);
  //默认未审核
  appClassInfo.authStatus = '0';
  appClassInfo.appId=fileAppClassInfo.value.appClassInfo.appId;
  appClassInfo.fileClassCode=fileAppClassInfo.value.appClassInfo.fileClassCode;
  appClassInfo.fileClassPath=fileAppClassInfo.value.appClassInfo.fileClassPath;
  let dateStr = fileAppClassInfo.value.appClassInfo.expireTime;
  var date=new Date(dateStr);
  appClassInfo.expireTime= date.getTime();
  $api.common.fileSend.post('fileAppClass0004', appClassInfo).then(() => {
    $messageBox.success('新增成功', '提示');
    state.appClassVisible=false;
    queryFileClass();
  }).catch(() => {});  
}



//------------设置给父组件调用的数据/方法(组件)-----------
defineExpose({setInitData,classInfo});
</script>
